//获得 leanCloud AV 的引用
const AV = require('../../libs/av-weapp-min.js');
Page({
  data: {
    provinceArr: [],
    cityArr: [],
    areaArr: [],
    provinceIdx: 0,
    cityIdx: 0,
    areaIdx: 0,
    currProvince: '',
    currCity: '',
    currArea:'',
    province:'',
    city:'',
    area:'',
    address:''
  },

  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: 'https://api.leancloud.cn/1.1/classes/AddressData/58a0518a128fe1006cee6c85',
      data: {},
      method: 'GET',
      header: {
        // 设置请求的 header，content-type 默认为 'application/json'
        'X-LC-Id': 'NifgaRbeW9zYQU8pP4zxPC9S-gzGzoHsz',
        'X-LC-Key': 'Ygv6uGw1TQmpB2Kk18m5TgvX'
      },
      success: function (res) {
        var proLen = res.data.data.p;
        var pArr = [];
        for (var i = 0; i < proLen.length; i++) {
          pArr.push(proLen[i])
        }
        that.setData({
          provinceArr: pArr
        })
      }
    })
  },

  // 省份选择
  bindProvinceChange: function (e) {
    var that = this;
    var province = this.data.provinceArr[e.detail.value];
    this.setData({
      provinceIdx: e.detail.value,
      currProvince: province
    })
    wx.request({
      url: 'https://api.leancloud.cn/1.1/classes/AddressData/58a0518a128fe1006cee6c85',
      data: {},
      method: 'GET',
      header: {
        // 设置请求的 header，content-type 默认为 'application/json'
        'X-LC-Id': 'NifgaRbeW9zYQU8pP4zxPC9S-gzGzoHsz',
        'X-LC-Key': 'Ygv6uGw1TQmpB2Kk18m5TgvX'
      },
      success: function (res) {
        var cities = res.data.data.c;
        var cityPro = cities[province];
        var cArr = [];
        for (var i = 0; i < cityPro.length; i++) {
          cArr.push(cityPro[i])
        }
        that.setData({
          cityArr: cArr,
          currProvince: province
        })
      }
    })
  },

  // 城市选择
  bindCityChange: function (e) {
    var that = this;
    var province = this.data.currProvince;
    var city = this.data.cityArr[e.detail.value];
    this.setData({
      cityIdx: e.detail.value,
      currCity : city
    })
    wx.request({
      url: 'https://api.leancloud.cn/1.1/classes/AddressData/58a0518a128fe1006cee6c85',
      data: {},
      method: 'GET',
      header: {
        // 设置请求的 header，content-type 默认为 'application/json'
        'X-LC-Id': 'NifgaRbeW9zYQU8pP4zxPC9S-gzGzoHsz',
        'X-LC-Key': 'Ygv6uGw1TQmpB2Kk18m5TgvX'
      },
      success: function (res) {
        var areas = res.data.data.a;
        var areaCy = areas[province + "-" + city];
        var aArr = [];
        for (var i = 0; i < areaCy.length; i++) {
          aArr.push(areaCy[i])
        }
        that.setData({
          areaArr: aArr
        })
      }
    })
  },

  // 地区选择
  bindAreaChange: function (e) {
    var that = this;
    var area = this.data.areaArr[e.detail.value];
    console.log(area)
    this.setData({
      areaIdx: e.detail.value,
      currArea : area
    })
  },

  // 分享
  onShareAppMessage: function () {
    // 用户点击右上角分享
    return {
      title: 'title', // 分享标题
      desc: 'desc', // 分享描述
      path: 'path' // 分享路径
    }
  },

  // 保存地址
  formSubmit: function(){
    var that = this;
    wx.setStorageSync('province', '江苏省 ');
    wx.navigateTo({
      url: '../account/account'
    })
  }
})

